<template>
	<view>
		<u-modal :show="show" title="提示" :show-cancel-button="false" :content="content" :closeOnClickOverlay="true"
			@close="close">
			<view slot="default" class="content">
				请授权获取手机号
			</view>
			<button open-type="getPhoneNumber" slot="confirmButton" class="wechat_btn" @getphonenumber="weixin_phone">
				确定
			</button>
		</u-modal>
	</view>
</template>

<script>
	import {
		getMemberPhone
	} from '@/service/api.js'

	export default {
		props: {
			phone_then: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				show: true
			}
		},
		methods: {
			close() {
				this.$emit('close')
			},
			// 获取手机号码
			weixin_phone(e) {
				console.log(e);
				let that = this;
				uni.login({
					success(r1) {
						if (e.detail.errMsg != 'getPhoneNumber:ok') {
							console.log('用户取消授权');
							uni.showToast({
								title: '授权失败',
								icon: 'none'
							})
							this.show = true
							return false;
						}

						let data = {
							encrypted_data : e.detail.encryptedData,
							iv: e.detail.iv,
							code : r1.code
						}

						getMemberPhone(data).then(r1 => {
							console.log(r1);
							if (r1.code == 200) {
								uni.setStorageSync('phone', true)
								that.$emit("toParent", that.phone_then)
								uni.showToast({
									title: '授权成功',
								})
							} else {
								uni.showToast({
									title: r1.msg,
								})
							}
						})
					}
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.wechat_btn {
		background-color: #fff;
		color: $u-primary;
		font-size: 32rpx;
		outline: none;
		height: 100%;
		line-height: inherit;
	}

	.wechat_btn::after {
		border: none;
	}

	.content {
		margin:60rpx auto;
		color: $u-content-color;
	}
</style>
